package com.bytz.modules.cms.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.system.base.controller.BytzController;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.excel.ImportExcelListener;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.order.entity.OrderOfflinePay;
import com.bytz.modules.cms.order.model.OrderOfflinePayModel;
import com.bytz.modules.cms.order.service.IAdminOrderOfflinePayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(tags = "汇款单")
@RequestMapping("/offlinePay")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.OderOfflinePay.EDIT + "')")
public class OrderOfflinePayController extends BytzController<OrderOfflinePay, IAdminOrderOfflinePayService> {

    @Autowired
    private IAdminOrderOfflinePayService orderOfflinePayService;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<OrderOfflinePayModel>> page(OrderOfflinePay offlinePay,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {

        return Result.ok(orderOfflinePayService.queryPage(new Page<>(pageNo, pageSize), offlinePay, req.getParameterMap()));
    }

    @ApiOperation(value = "查询单条", notes = "查询单条")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<OrderOfflinePay> queryById(@RequestParam String id) {

        return Result.ok(orderOfflinePayService.getById(id));
    }

    @ApiOperation(value = "保存或编辑", notes = "保存或编辑")
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    public Result<OrderOfflinePay> saveOrUpdate(@RequestBody OrderOfflinePay offlinePay) {

        return Result.ok(orderOfflinePayService.saveOrUpdateSaveObject(offlinePay, true));
    }

    @ApiOperation(value = "获取导入模板", notes = "获取导入模板")
    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {

        ExcelUtil.exportTemp("批量导入汇款单", "Sheet1", OrderOfflinePay.class, response);
    }

    @ApiOperation(value = "批量导入", notes = "批量导入")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request) {

        return Result.ok(ExcelUtil.importGroup(request, OrderOfflinePay.class, new ImportExcelListener<>((list) -> {
            orderOfflinePayService.importExcel(list);
        })));
    }

    @ApiOperation(value = "删除一条", notes = "删除一条")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Boolean> delete(@RequestParam String id) {

        return Result.ok(orderOfflinePayService.checkAndRemoveById(id));
    }

    @ApiOperation(value = "导出列表", notes = "导出列表")
    @RequestMapping(value = "/exportXls", method = RequestMethod.GET)
    public void exportXls(OrderOfflinePay orderOfflinePay, HttpServletRequest req, HttpServletResponse response) throws Exception {
        orderOfflinePayService.exportExcel(orderOfflinePay,req.getParameterMap(),response);
    }

}